package com.own.business.message.center.business.util;

import com.own.business.message.center.business.service.MessageCenterContentService;
import com.own.business.message.center.entity.po.MessageCenterContent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * MessageCenterUtil
 *
 * @author chenxueli
 * @date 2023-11-14 18:59:00
 */
@Slf4j
@Component
public class MessageCenterUtil {

    @Resource
    private MessageCenterContentService messageCenterContentService;

    /**
     * 发送消息给用户
     *
     * @param messageKey    消息key
     * @param userId        用户id
     * @param content       消息内容
     * @param detailContent 详细内容（json）
     * @param businessId    业务id
     */
    public void send(String messageKey, Long userId, String content, String detailContent, Long businessId) {
        send(messageKey, userId, content, detailContent, businessId, null);
    }

    /**
     * 发送消息给用户
     *
     * @param messageKey    消息key
     * @param userIdList    用户ids
     * @param content       消息内容
     * @param detailContent 详细内容（json）
     * @param businessId    业务id
     */
    public void send(String messageKey, Collection<Long> userIdList, String content, String detailContent, Long businessId) {
        send(messageKey, userIdList, content, detailContent, businessId, null);
    }

    /**
     * 发送消息给用户
     *
     * @param messageKey    消息key
     * @param userId        用户id
     * @param content       消息内容
     * @param detailContent 详细内容（json）
     * @param businessId    业务id
     * @param senderUserId  发送者id
     */
    public void send(String messageKey, Long userId, String content, String detailContent, Long businessId, Long senderUserId) {
        var po = new MessageCenterContent()
                .setFkReceiveId(userId)
                .setContent(content)
                .setMessageKey(messageKey)
                .setDetailContent(detailContent)
                .setFkBusinessId(businessId)
                .setIsRead(0);
        po.setCreateUserId(senderUserId);
        messageCenterContentService.add(po);
    }

    /**
     * 发送消息给用户
     *
     * @param messageKey    消息key
     * @param userIdList    用户ids
     * @param content       消息内容
     * @param detailContent 详细内容（json）
     * @param businessId    业务id
     * @param senderUserId  发送者id
     */
    public void send(String messageKey, Collection<Long> userIdList, String content, String detailContent, Long businessId, Long senderUserId) {
        var addList = userIdList
                .stream()
                .distinct()
                .map(userId -> new MessageCenterContent()
                        .setFkReceiveId(userId)
                        .setContent(content)
                        .setMessageKey(messageKey)
                        .setDetailContent(detailContent)
                        .setFkBusinessId(businessId)
                        .setIsRead(0))
                .peek(po -> po.setCreateUserId(senderUserId))
                .toList();
        messageCenterContentService.addPoBatch(addList);
    }

}
